package com.bwie.controller;

import com.alipay.api.AlipayApiException;
import com.bwie.service.OrderService;

import com.bwie.utils.QRCodeUtil;
import com.bwie.utils.R;
import com.bwie.utils.WxPayUtils;
import com.bwie.utils.ZhifubaoUtill;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author lenovo
 */
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private ZhifubaoUtill zhifubaoUtill;
    @Autowired
    private WxPayUtils wxPayUtils;

    @PostMapping("/list")
    public R orderList(@RequestHeader String token){
        return orderService.orderList(token);
    }

    @GetMapping("wxpay/{orderId}")
    public void wxpay(@PathVariable String orderId, HttpServletResponse response){
        //微信支付
        String wxPay = wxPayUtils.wxPay(orderId);
        QRCodeUtil.createQRCode(response, wxPay);
    }


    @GetMapping("/alipay/{orderId}/{price}")
    public String alipay(@PathVariable String orderId,@PathVariable String price) throws AlipayApiException {
        String pay = zhifubaoUtill.pay(orderId, price);
        return pay;
    }

    @GetMapping("/result")
    public R result(HttpServletRequest request) throws AlipayApiException {
        String outTradeNo = request.getParameter("out_trade_no");
        Boolean aBoolean = zhifubaoUtill.queryResult(outTradeNo);

        if(aBoolean){
            return R.OK("支付成功");
        }else{
            return R.ERROR(500,"支付失败");
        }
    }
}
